package com.jlcindia.util;

import java.io.Serializable;

import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

public class CHibernateTemplate {
	
	public static Object save(Object obj){
		Object id = null;
		try{
			SessionFactory sf = CHibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			Transaction tx = session.beginTransaction();
			id = session.save(obj);
			tx.commit();
			session.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return id;
	}
	
	public static void update(Object obj){
		try {
			SessionFactory sf = CHibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			Transaction tx = session.beginTransaction();
			session.update(obj);
			tx.commit();
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void delete(Class cls,Serializable s) {
		try{
			SessionFactory sf = CHibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			Transaction tx = session.beginTransaction();
			Object o =session.load(cls, s);
			session.delete(o);
			tx.commit();
			session.close();
		}
		catch(Exception e){
 			e.printStackTrace();
		}
	}
	
	public static Object load(Class cls,Serializable s) {
		Object o = null;
		try{
			SessionFactory sf = CHibernateUtil.getSessionFactory();
			Session session = sf.openSession();
			Transaction tx = session.beginTransaction();
			o =session.load(cls, s);
			tx.commit();
			session.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return o;
	}

}
